package snippet;

public class T2 {

	 public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		 int a=0,b=0;
		 if(l1!=null)a=l1.val;
		 if(l2!=null)b=l2.val;
		 int n=a+b;
		 ListNode c1=new ListNode(n);
		 ListNode x=null,y=null;
		 if(l1!=null&&l1.next!=null) {
			 x=l1.next;
		 }
		 if(l2!=null&&l2.next!=null) {
			 y=l2.next;
		 }
		 if(n>9) {
			 n=n%10;
			c1.val=n;
			if(x!=null) {
				x.val++;
			}else {
				x=new ListNode(1);
				l1.next=x;
			}
			
		 }
		 if(x!=null||y!=null) {
			
			c1.next= addTwoNumbers(x,y);
			
		 }
		
	        return c1;
	    }
	 
	 
	 public static void main(String[] args) {
	 ListNodeUtil lu= new ListNodeUtil();

	 ListNode a = lu.addNumber(1);
	 ListNode b = lu.addNumber(99);
	 ListNode x = addTwoNumbers(a,b);
	 lu.print(x);
	}
}
